home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / T U R B O Language / Turbo C Tools v6.0 / INCLUDE / BUTIL.H < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  17.0 KB  |  477 lines

  1. /**
  2. *
  3. *  BUTIL.H    Header file for Turbo C TOOLS Utility functions
  4. *
  5. *  Version 6.00  (C)Copyright Blaise Computing Inc.  1987-1989
  6. *
  7. **/
  8.  
  9. #ifndef DEF_BUTIL        /* Prevent redefinition.        */
  10.  
  11. #include <string.h>
  12. #include <stdlib.h>
  13.  
  14. /* needs dos.h */        /* For inp(), outp(), _osmajor,     */
  15.                 /* _osminor, and _psp.            */
  16.  
  17.  
  18. /********************************************************************/
  19. /* Definitions of data types.                        */
  20. /********************************************************************/
  21.  
  22. typedef struct            /* HALFREGS:  two halves of a 16-bit*/
  23. {                /* general register.            */
  24.     unsigned char l;        /* Lower 8 bits.            */
  25.     unsigned char h;        /* Upper 8 bits.            */
  26. } HALFREGS;
  27.  
  28. typedef union            /* DOUBLREG:  Two ways to express   */
  29. {                /* a 16-bit general register:        */
  30.     HALFREGS hl;        /* Two separated halves.        */
  31.     unsigned x;         /* 16-bit unsigned quantity.        */
  32. } DOUBLREG;
  33.  
  34. typedef struct            /* ALLREG:  complete CPU state        */
  35. {                /*   (This must match version        */
  36.                 /*   in ISCALINT.ASM.)            */
  37.     DOUBLREG ax,bx,cx,dx;    /* General registers.            */
  38.     unsigned si,di;        /* Index   registers.            */
  39.     unsigned ds,es,ss,cs;    /* Segment registers.            */
  40.     unsigned flags,bp,sp,ip;    /* Other   registers.            */
  41. } ALLREG;
  42.  
  43. struct dreg            /* DOSREG:  registers for call        */
  44. {                /*   to DOS.                */
  45.     unsigned ax,bx,cx,dx,si,di,ds,es;
  46. };
  47. #define DOSREG    struct dreg
  48.  
  49. typedef struct segads        /* ADS:  Physical address        */
  50. {                /*   expressed as segment and offset*/
  51.     unsigned r;         /* Offset.                */
  52.     unsigned s;         /* Segment.                */
  53. } ADS;
  54.  
  55. struct address            /* Physical address.            */
  56. {                /*   expressed as segment and offset*/
  57.     unsigned r;         /* Offset.                */
  58.     unsigned s;         /* Segment.                */
  59. };
  60.  
  61.  
  62. /********************************************************************/
  63. /* Flag bits for use with ALLREG structure.                */
  64. /********************************************************************/
  65.  
  66. #define CF_FLAG     0x0001    /* Carry flag.                */
  67. #define PF_FLAG     0x0002    /* Parity flag (1 means even).        */
  68. #define AF_FLAG     0x0010    /* Auxiliary carry flag.        */
  69. #define ZF_FLAG     0x0040    /* Zero flag (1 means zero).        */
  70. #define SF_FLAG     0x0080    /* Sign flag (1 means negative).    */
  71. #define TF_FLAG     0x0100    /* Trap flag (1 means single step). */
  72. #define IF_FLAG     0x0200    /* Interrupt enable flag.        */
  73. #define DF_FLAG     0x0400    /* Direction flag (0 means up).     */
  74. #define OF_FLAG     0x0800    /* Overflow flag (1 means overflow).*/
  75.  
  76. #define DEF_FLAGS   (IF_FLAG)    /* Commonest value for flags:        */
  77.                 /*   interrupts enabled,        */
  78.                 /*   single-stepping off,        */
  79.                 /*   direction up.            */
  80.  
  81.  
  82. /********************************************************************/
  83. /* Handy system constants and addresses.                */
  84. /********************************************************************/
  85.  
  86. #define UTDOSINT 0x21        /* Standard DOS gate interrupt.     */
  87.  
  88.         /* Address of PC model code.                */
  89. #define UTMODELADDR (uttofaru (0xffff, 0xe))
  90.  
  91.         /* Address of PC BIOS clock tick count.         */
  92. #define UTCLOCKADDR (uttofaru (0x40, 0x6c))
  93.  
  94.         /* Address of PC BIOS clock rollover flag.        */
  95. #define UTROLLADDR (uttofaru (0x40, 0x70))
  96.  
  97.         /* Possible values for IBM Model Code:            */
  98. #define  IBM_PC  ((char) 0xff)    /* PC.                    */
  99. #define  IBM_XT  ((char) 0xfe)    /* PC-XT or Portable PC.        */
  100. #define  IBM_JR  ((char) 0xfd)    /* IBM PC-Jr.                */
  101. #define  IBM_AT  ((char) 0xfc)    /* IBM (or compat.) PC-AT.        */
  102. #define  IBM_X2  ((char) 0xfb)    /* PC-XT with 256/640 motherboard.  */
  103. #define  IBM_CV  ((char) 0xf9)    /* PC Convertible.            */
  104. #define  IBM_30  ((char) 0xfa)    /* PS/2 model 30.            */
  105. #define  IBM_50  ((char) 0xfc)    /* PS/2 models 50, 60.            */
  106. #define  IBM_80  ((char) 0xf8)    /* PS/2 model 80.            */
  107.  
  108.  
  109. /********************************************************************/
  110. /* Global variables maintained by UT functions and macros.        */
  111. /********************************************************************/
  112.  
  113.          /* Contains address of DOS critical section flag,  */
  114.          /* after UTCRIT is called.                */
  115. extern unsigned char far *b_critad;
  116.  
  117. extern char b_pcmodel;        /* IBM PC Model Code.            */
  118. extern unsigned char b_submod;    /* IBM PC submodel code, or 0 if    */
  119.                 /*   unavailable.            */
  120. extern unsigned char b_biosrev; /* IBM PC BIOS revision level, or 0 */
  121.                 /*   if unavailable.            */
  122.  
  123. extern int b_ansdis;        /* Nonzero if UTANSI has disabled   */
  124.                 /*   ANSI.SYS.                */
  125.  
  126.  
  127. /********************************************************************/
  128. /* Constant values for UTINTFLG, UTAMOVE, UTSAFCPY, UTCTLBRK, UTANSI*/
  129. /********************************************************************/
  130.  
  131. #define UT_INTON  1        /* Values to use with UTINTFLG.     */
  132. #define UT_INTOFF 0
  133.  
  134.                 /* Values to use with UTAMOVE.        */
  135. #define UT_MVFORWARD  0     /*     Increasing memory addresses. */
  136. #define UT_MVBACKWARD 1     /*     Decreasing memory addresses. */
  137.  
  138.                 /* Values to use with UTSAFCPY.     */
  139.                 /*                    */
  140. #define UT_SRC_STRADDLE 1    /* Source buffer straddles segment  */
  141.                 /*   boundary, target okay.        */
  142. #define UT_TGT_STRADDLE 2    /* Target buffer straddles segment  */
  143.                 /*   boundary, source okay.        */
  144.                 /* Both source & target straddle.   */
  145. #define UT_2_STRADDLE    ((UT_SRC_STRADDLE) + (UT_TGT_STRADDLE))
  146.  
  147.                 /* Values to use with UTCTLBRK.     */
  148. #define  CBRK_SET    1    /*     Set CTRL-BREAK state.        */
  149. #define  CBRK_GET    0    /*     Return CTRL-BREAK state.     */
  150. #define  CBRK_ON    1    /*     Check CTRL-BREAK at every    */
  151.                 /*       DOS call.            */
  152. #define  CBRK_OFF    0    /*     Check CTRL-BREAK only when   */
  153.                 /*       accessing standard input,*/
  154.                 /*       output, print, or        */
  155.                 /*       auxiliary device.        */
  156.  
  157.                 /* Values to use with UTANSI.        */
  158.                 /*                    */
  159.                 /* As command:        As result:        */
  160. #define UT_ANS_ABSENT    (-2)    /*   (Invalid.)       Not detected. */
  161. #define UT_ANS_ENABLE    (-1)    /*   Enable.          Active.        */
  162. #define UT_ANS_DISABLE      0    /*   Disable.          Disabled.     */
  163. #define UT_BAD_OPT      1    /*   (Invalid.)       Bad option.   */
  164. #define UT_ANS_DETECT      2    /*   Sense presence.  (Impossible.) */
  165. #define UT_ANS_HANDLES      3    /*   (Invalid.)       No handles.   */
  166.  
  167. /********************************************************************/
  168. /* Constant values for UTSQZSCN, UTUNSQZ.                */
  169. /********************************************************************/
  170.  
  171. #define UT_CHAR_KEY    0xFF
  172. #define UT_ATTR_KEY    0xFE
  173.  
  174. /********************************************************************/
  175. /* Macros to combine and extract words, bytes, and nybbles.        */
  176. /********************************************************************/
  177.  
  178. #define uthiword(a)   (((a)>>16)&0xffffL)   /* High word of long a. */
  179. #define utloword(a)   ((a)&0xffffL)        /* Low  word of long a. */
  180.  
  181.                 /* Combine high word a, low  word b.*/
  182. #define utwdlong(a,b) ((((0xffffL&(long)(a)))<<16)|             \
  183.                (0xffffL&(long)(b)))
  184.  
  185. #define uthibyte(a)   (((a)>>8)&0x00ff) /* High byte of word a.     */
  186. #define utlobyte(a)   ((a)&0x00ff)    /* Low    byte of word a.     */
  187.  
  188.                 /* Combine high byte a, low byte b. */
  189. #define utbyword(a,b) ((((a)&0x00ff)<<8)|((b)&0x00ff))
  190.  
  191. #define uthinyb(a)    (((a)>>4)&0x000f) /* High nybble of byte a.   */
  192. #define utlonyb(a)    ((a)&0x000f)    /* Low nybble  of byte a.   */
  193.  
  194.                 /* Combine high nybble a, low        */
  195.                 /* nybble b.                */
  196. #define utnybbyt(a,b) ((((a)&0x000f)<<4)|((b)&0x000f))
  197.  
  198.  
  199. /********************************************************************/
  200. /* Macros for pointer manipulation.                    */
  201. /********************************************************************/
  202.  
  203.             /* Construct far void pointer.            */
  204. #define uttofaru(seg,off) ((void far *)                  \
  205.                ((((unsigned long) (unsigned int)         \
  206.                  (seg)) << 16L) |                 \
  207.                ((unsigned long) (unsigned int) (off))))
  208.  
  209.             /* Compute offset  of memory pointed to.    */
  210. #define utoff(p) ((unsigned int) (p))
  211.  
  212.             /* Compute segment of memory pointed to.    */
  213. #define utseg(p) ((unsigned int)                     \
  214.           (((unsigned long) (const void far *) (p)) >> 16L))
  215.  
  216.  
  217.             /* Make a far pointer to a type.        */
  218. #define uttofar(seg,off,type) ((type far *) uttofaru((seg),(off)))
  219.  
  220.             /* Normalize a pointer.             */
  221. #define utnorm(p,type) (uttofar (utseg (p) + (utoff (p) >> 4),         \
  222.                 (utoff (p) & 0x000f),             \
  223.                 type))
  224.  
  225.             /* Return 20-bit address in a pointer.        */
  226. #define utplong(p) ((((unsigned long) utseg (p)) << 4L) +         \
  227.             ((unsigned long) utoff (p)) &             \
  228.             0xfffffL)
  229.  
  230.  
  231. /********************************************************************/
  232. /* Macros for low-level memory manipulation.                */
  233. /********************************************************************/
  234.  
  235.             /* Return byte pointed to by pointer.        */
  236. #define utpeekb(p) (*((const unsigned char far *) (p)))
  237.  
  238.             /* Return word pointed to by pointer.        */
  239. #define utpeekw(p) (*((const unsigned int far *) (p)))
  240.  
  241.             /* Put byte in location pointed to.        */
  242. #define utpokeb(p,bval) (*((unsigned char far *) (p)) = (bval))
  243.  
  244.             /* Put word in location pointed to.        */
  245. #define utpokew(p,uval) (*((unsigned int far *) (p)) = (uval))
  246.  
  247.             /* Fetch N bytes from a far pointer.        */
  248. #define utpeekn(pf,pn,clen) (utmovmem ((const char far *) (pf),     \
  249.                        (char far *) (pn),        \
  250.                        (clen)))
  251.  
  252.             /* Put N bytes to a far pointer.        */
  253. #define utpoken(pn,pf,clen) (utmovmem ((const char far *) (pn),      \
  254.                        (pf),                 \
  255.                        (clen)))
  256.  
  257.  
  258. /********************************************************************/
  259. /* Macros for communication with ports.                 */
  260. /********************************************************************/
  261.  
  262.             /* Return a byte from a port.            */
  263. #define utinp(port) (inp (port))
  264.  
  265.             /* Put a byte in a port.            */
  266. #define utoutp(port,bval) ((void) outp ((port), (int) (bval)))
  267.  
  268.  
  269. /********************************************************************/
  270. /* Miscellaneous macros.                        */
  271. /********************************************************************/
  272.  
  273. #define NIL        ((void *) 0)    /* Universal invalid data   */
  274.                     /* pointer.            */
  275.  
  276.                     /* Far version of NIL.        */
  277. #define FARNIL        ((void far *) 0)
  278.  
  279. #define utsign(a)    ((a)>0?1:((a)==0?0:-1)) /* Arithmetic sign. */
  280. #define utalarm()    utsound(450,9)        /* Beep for 1/2 sec.*/
  281. #define utskip        printf("\n")            /* CR/LF.           */
  282.  
  283.  
  284.         /* Allocate and zero a data object of a given type, */
  285.         /* return a pointer to it.                */
  286. #define utalloc(type) ((type *) calloc(1,sizeof(type)))
  287.  
  288.  
  289.         /* Copy a data object, and return a pointer to        */
  290.         /* destination.                     */
  291. #define utcopy(to,from,type) ((type *)                     \
  292.         memmove ((to),                         \
  293.              (from),                     \
  294.              sizeof (type)))
  295.  
  296.  
  297.         /* Return DOS version numbers.                */
  298. #define utdosmajor ((unsigned char) uthibyte (utdosver))
  299. #define utdosminor ((unsigned char) utlobyte (utdosver))
  300.  
  301. #define utdosver ((unsigned int) utbyword (_osmajor, _osminor))
  302.  
  303.  
  304.         /* Check for null pointer assignments.            */
  305. #if (defined(BCHKNIL) || !(defined(BNOCHKNIL)))
  306. #define utchknil() (utnulchk()                         \
  307.             ? (fprintf (stderr,                  \
  308.                "Null pointer assignment detected in <%s:%d>\n",\
  309.                __FILE__, __LINE__),                 \
  310.                exit (1),                     \
  311.                (1))                         \
  312.             : (0))
  313. #else
  314. #define utchknil() ((void) 0)
  315. #endif
  316.  
  317.             /* Return the segment address of the        */
  318.             /* program's PSP.                           */
  319. #define utpspseg ((unsigned) (_psp))
  320.  
  321.             /* Return 0 if DOS is not ready, 1 if it is.*/
  322.             /* Always returns 0 is UTCRIT has not        */
  323.             /* executed yet.                */
  324. #define utdosrdy() ((b_critad != FARNIL)                 \
  325.              ? (utpeekb (b_critad) == 0)             \
  326.              : (0))
  327.  
  328.             /* Enable interrupts, and return old        */
  329.             /* interrupt flag value.            */
  330. #define utinton() (utintflg (UT_INTON))
  331.  
  332.             /* Disable interrupts, and return old        */
  333.             /* interrupt flag value.            */
  334. #define utintoff() (utintflg (UT_INTOFF))
  335.  
  336.             /* Turn the speaker off.            */
  337. #define utspkoff() (utspkr (0))
  338.  
  339.             /* Turn the speaker on, with a specified    */
  340.             /* frequency sound to be produced.        */
  341. #define utspkon(freq) (utspkr (freq))
  342.  
  343.             /* Make a sound of "freq" frequency with the*/
  344.             /* speaker for a specified duration.        */
  345. #define utsound(freq, dura) (                         \
  346.         utspkon (freq),                      \
  347.         utsleep (dura),                      \
  348.         utspkoff ())
  349.  
  350.  
  351. /********************************************************************/
  352. /* Macros to perform range checking and limiting.            */
  353. /********************************************************************/
  354.  
  355. /* Beware:  the following three macros (UTMAX, UTMIN, and UTABS)    */
  356. /* may have side effects because they evaluate their arguments more */
  357. /* than once.                                */
  358.  
  359.                 /* Maximum of two values.        */
  360. #define utmax(a,b) ((a) > (b) ? (a) : (b))
  361. #ifndef max
  362. #define max(a,b) utmax(a,b)
  363. #endif
  364.  
  365.                 /* Minimum of two values.        */
  366. #define utmin(a,b) ((a) <= (b) ? (a) : (b))
  367. #ifndef min
  368. #define min(a,b) utmin(a,b)
  369. #endif
  370.  
  371. #define utabs(x) ((x)<0 ? -(x) : (x))    /* Absolute value.        */
  372.  
  373.             /* Return 1 if a is outside the range        */
  374.             /* defined by l and h, 0 if not.        */
  375. #define utrange(a,l,h)    (((a) < (l)) || ((a) > (h)))
  376. #define utoutrng(a,l,h) utrange(a,l,h)
  377.  
  378.             /* Confine a to the range defined by b and c*/
  379. #define utbound(a,b,c)                             \
  380.     {                                     \
  381.     register int uttemp;                         \
  382.                                      \
  383.     if ((a) < (uttemp = (b)) || (a) > (uttemp = (c)))         \
  384.         (a) = uttemp;                         \
  385.     }
  386.  
  387.             /* Prevent a from exceeding b.            */
  388. #define utuplim(a,b)                             \
  389.     {                                     \
  390.     register int uttemp;                         \
  391.                                      \
  392.     if ((a) > (uttemp = (b)))                     \
  393.         (a) = uttemp;                         \
  394.     }
  395.  
  396.             /* Prevent a from being less than b.        */
  397. #define utlowlim(a,b)                             \
  398.     {                                     \
  399.     register int uttemp;                         \
  400.                                      \
  401.     if ((a) < (uttemp = (b)))                     \
  402.         (a) = uttemp;                         \
  403.     }
  404.  
  405.  
  406. /********************************************************************/
  407. /* Function declarations.                        */
  408. /********************************************************************/
  409.  
  410. void cdecl utamove(const char far *,/* Copy memory from far pointer */
  411.             char far *,     /* source to far pointer target.*/
  412.             unsigned int,   /* Does not handle overlap.     */
  413.             int);        /*                    */
  414.                     /*                    */
  415. int cdecl utctlbrk (int,int);        /* Set or return state of DOS   */
  416.                     /* CTRL-BREAK checking.        */
  417.                     /*                    */
  418. #ifndef utdosver            /*                    */
  419. int cdecl utdosver (void);        /* Return the DOS major, minor  */
  420. #endif                    /* version number as 16-bit.    */
  421.                     /*                    */
  422. int cdecl utgetclk (long *);        /* Return the BIOS clock roll   */
  423.                     /* flag and tick count.        */
  424.                     /*                    */
  425. int cdecl utintflg (int);        /* Set interrupt state, return  */
  426.                     /* old state.            */
  427.                     /*                    */
  428. unsigned int cdecl utnulchk (void); /* Check for NIL-pointer        */
  429.                     /* assignments.            */
  430.                     /*                    */
  431. unsigned cdecl utsleep (unsigned);  /* Suspend processing for        */
  432.                     /* specified duration.        */
  433.                     /*                    */
  434. void cdecl utspkr (unsigned);        /* Speaker control.         */
  435.                     /*                    */
  436. int cdecl utansi(int);            /* Detect, disable, or        */
  437.                     /*     re-enable ANSI.SYS.        */
  438.                     /*                    */
  439. void cdecl utmovmem(            /* Copy block of memory, using  */
  440.    const char far *, char far *,    /* far pointer source and        */
  441.              unsigned int); /* target.                */
  442.                     /*                    */
  443. int cdecl utsafcpy(const void far *,/* Copy block of memory if        */
  444.            void far *,        /*     source & target buffers    */
  445.            unsigned int);   /*     don't straddle segment     */
  446.                     /*     boundaries.            */
  447.                     /*                    */
  448. unsigned char far * cdecl        /* Return the address of the    */
  449.           utcrit (void);    /* DOS critical section flag.   */
  450.                     /* Also puts the value in        */
  451.                     /* b_critad.            */
  452.                     /*                    */
  453. char cdecl utmodel(void);        /* Return machine model code.   */
  454.                     /*                    */
  455. unsigned long cdecl uttim2tk        /* Compute a tick value for a   */
  456.                (int, int,   /* given time of day.        */
  457.             int, int);  /*                    */
  458.                     /*                    */
  459. void cdecl uttk2tim(unsigned long,  /* Compute a time value (in     */
  460.             int *, int *,   /* hours, minutes, seconds,     */
  461.             int *, int *);  /* hundredths) for a given        */
  462.                     /* tick value.            */
  463.                     /*                    */
  464. int cdecl utsqzscn(const char far *,/* Compress a screen image.     */
  465.            char *, int,     /*                    */
  466.            int);        /*                    */
  467.                     /*                    */
  468. int cdecl utunsqz(const char *,     /* Uncompress a screen image.   */
  469.           char far *, int,  /*                    */
  470.           int);         /*                    */
  471.  
  472.  
  473. #define DEF_BUTIL  1    /* Prevent second reading of these        */
  474.             /* definitions.                 */
  475.  
  476. #endif            /* Ends "#ifndef DEF_BUTIL".                */
  477.